package com.jdbc.senior.dao.impl;

import com.jdbc.senior.dao.BaseDAO;
import com.jdbc.senior.dao.EmployeeDao;
import com.jdbc.senior.pojo.Employee;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

public class EmployeeDaoImpl extends BaseDAO implements EmployeeDao {
    @Override
    public List<Employee> selectAll() {
        String sql = "select emp_id as empId, emp_name as empName, emp_salary as empSalary, emp_age as empAge\n" +
                "from t_emp";
        try {
            return executeQuery(sql, Employee.class, null);
        } catch (SQLException | NoSuchMethodException | InvocationTargetException | InstantiationException |
                 IllegalAccessException | NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Employee selectById(Integer empId) {
        String sql = "select emp_id as empId, emp_name as empName, emp_salary as empSalary, emp_age as empAge\n" +
                "from t_emp where emp_id = ?";
        try {
            return executeQueryBean(Employee.class, sql, empId);
        } catch (SQLException | NoSuchFieldException | InvocationTargetException | NoSuchMethodException |
                 InstantiationException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(Employee employee) {
        String sql = "insert into t_emp(emp_name, emp_salary, emp_age)\n" +
                "values (?, ?, ?)";
        try {
            return executeUpdate(sql, employee.getEmpName(), employee.getEmpSalary(), employee.getEmpAge());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int update(Employee employee) {
        return 0;
    }

    @Override
    public int delete(Integer empId) {
        return 0;
    }
}
